<template>
  <div class="home">
    <!-- 轮播图区域 -->
    <section class="banner" >
      <div
        id="carouselExampleCaptions"
        class="carousel slide"
        data-bs-ride="carousel"
      >
        <div class="carousel-indicators">
          <button
            v-for="(item, index) in swiperList"
            :key="item.id"
            type="button"
            :data-bs-target="`#carouselExampleCaptions`"
            :data-bs-slide-to="index"
            :class="{ active: index === 0 }"
            :aria-current="index === 0 ? 'true' : 'false'"
            :aria-label="`Slide ${index + 1}`"
          ></button>
        </div>
        <div class="carousel-inner">
          <div
            v-for="(item, index) in swiperList"
            :key="item.id"
            :class="['carousel-item', { active: index === 0 }]"
          >
            <img
              :src="item.image"
              class="d-block w-100"
              :alt="item.title"
            />
            <div class="carousel-caption d-none d-md-block">
              <h5>{{ item.title }}</h5>
              <p>{{ item.description }}</p>
            </div>
          </div>
        </div>
        <button
          class="carousel-control-prev"
          type="button"
          data-bs-target="#carouselExampleCaptions"
          data-bs-slide="prev"
        >
          <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          <span class="visually-hidden">Previous</span>
        </button>
        <button
          class="carousel-control-next"
          type="button"
          data-bs-target="#carouselExampleCaptions"
          data-bs-slide="next"
        >
          <span class="carousel-control-next-icon" aria-hidden="true"></span>
          <span class="visually-hidden">Next</span>
        </button>
      </div>
    </section>

    <!-- 热门目的地 -->
    <section class="popular-destinations py-5 bg-light">
      <div class="container">
        <div class="section-header text-center mb-5">
          <h2 class="section-title">热门目的地</h2>
          <p class="section-subtitle">最受旅行者欢迎的目的地</p>
        </div>
        <div class="row">
          <div
            v-for="destination in popularDestinations"
            :key="destination.id"
            class="col-lg-3 col-md-4 col-sm-6 mb-4"
          >
            <div class="destination-card">
              <div class="destination-image">
                <img :src="destination.image" :alt="destination.name" />
                <div class="destination-overlay">
                  <h5 class="destination-name">{{ destination.name }}</h5>
                  <p class="destination-posts">{{ destination.postCount }} 篇游记</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>


        <!-- 精选游记 -->
    <section class="featured-posts py-5">
      <div class="container">
        <div class="section-header text-center mb-5">
          <h2 class="section-title">精选游记</h2>
          <p class="section-subtitle">发现旅行者们的精彩故事</p>
        </div>
        <div class="row">
          <div
            v-for="post in featuredPosts"
            :key="post.id"
            class="col-lg-4 col-md-6 mb-4"
          >
            <div class="post-card" @click="goToPost(post.id)">
              <div class="post-image">
                <img :src="post.images[0]" :alt="post.title" />
                <div class="post-overlay">
                  <div class="post-stats">
                    <span><i class="bi bi-eye"></i> {{ post.viewCount }}</span>
                    <span><i class="bi bi-heart"></i> {{ post.likeCount }}</span>
                  </div>
                </div>
              </div>
              <div class="post-content">
                <h5 class="post-title">{{ post.title }}</h5>
                <p class="post-location">
                  <i class="bi bi-geo-alt"></i>
                  {{ post.location }}
                </p>
                <p class="post-excerpt">{{ truncateText(post.content, 100) }}</p>
                <div class="post-meta">
                  <div class="author-info">
                    <img :src="post.author.avatar" :alt="post.author.username" class="author-avatar" />
                    <span class="author-name">{{ post.author.username }}</span>
                  </div>
                  <span class="post-date">{{ formatDate(post.createTime) }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- <div class="text-center mt-4">
          <router-link to="/posts" class="btn btn-outline-primary btn-lg">
            查看更多游记
          </router-link>
        </div> -->
      </div>
    </section>
    <div class="demo"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElInput, ElButton, ElIcon, ElTag, ElMessage } from 'element-plus'
import { Search, LocationFilled } from '@element-plus/icons-vue'
import { getSwiperList, getHotPosts, getStatistics , getNotes} from '@/api/data'

// 导入图片资源
import { images, defaultAvatar } from '@/assets/images'

const router = useRouter()

// 响应式数据
const searchKeyword = ref('')
const swiperList = ref([])
const featuredPosts = ref([])
const loading = ref(false)

// 热门搜索关键词
const hotKeywords = ref(['北京', '上海', '成都', '西安', '三亚', '云南', '新疆', '西藏'])

// 热门目的地（示例数据）
const popularDestinations = ref([
  {
    id: 1,
    name: '北京',
    image: images.img20221106190735,
    postCount: 1280
  },
  {
    id: 2,
    name: '上海',
    image: images.img90,
    postCount: 956
  },
  {
    id: 3,
    name: '成都',
    image: images.img980,
    postCount: 743
  },
  {
    id: 4,
    name: '西安',
    image: images.imgOIP,
    postCount: 624
  }
])


// 搜索功能
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({
      name: 'TravelPosts',
      query: { keyword: searchKeyword.value.trim() }
    })
  } else {
    ElMessage.warning('请输入搜索关键词')
  }
}

// 跳转到游记详情
const goToPost = (postId: number) => {
  router.push({ name: 'PostDetail', params: { id: postId } })
}

// 文本截断
const truncateText = (text: string, length: number) => {
  if (text.length <= length) return text
  return text.substring(0, length) + '...'
}

// 格式化日期
const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
      // 使用默认数据
      swiperList.value = [
        {
          id: 1,
          title: '探索美丽的自然风光',
          description: '发现世界各地的绝美景色',
          image: images.img20221106190735
        },
        {
          id: 2,
          title: '体验异域文化之旅',
          description: '感受不同文化的独特魅力',
          image: images.img90
        },
        {
          id: 3,
          title: '享受美食与美景',
          description: '品味当地特色美食与风景',
          image: images.img980
        }
      ]


      // 使用默认数据
      featuredPosts.value = [
        {
          id: 1,
          title: '云南大理洱海之旅',
          content: '在大理的日子里，每天都被洱海的美景所震撼...',
          location: '云南大理',
          images: [images.img20221106190735],
          author: {
            id: 1,
            username: '旅行者小李',
            avatar: defaultAvatar
          },
          viewCount: 1258,
          likeCount: 89,
          createTime: '2024-01-15'
        },
        {
          id: 2,
          title: '北京胡同文化探索',
          content: '走在北京的胡同里，感受老北京的文化韵味...',
          location: '北京',
          images: [images.img90],
          author: {
            id: 2,
            username: '文化爱好者',
            avatar: defaultAvatar
          },
          viewCount: 956,
          likeCount: 67,
          createTime: '2024-01-12'
        },
        {
          id: 3,
          title: '成都美食之旅',
          content: '成都的美食让人流连忘返，每一道菜都是惊喜...',
          location: '四川成都',
          images: [images.img980],
          author: {
            id: 3,
            username: '美食达人',
            avatar: defaultAvatar
          },
          viewCount: 743,
          likeCount: 52,
          createTime: '2024-01-10'
        }
      ]
  } catch (error) {
    console.error('加载数据失败:', error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
/* .demo{
margin: 10px;
     width:0;
    height:0;
    border: 100px solid transparent;
    border-top: 100px solid red;
} */
.banner {
  width: 100%;
  height: 60vh;
  mask-image: linear-gradient(rgb(0, 0, 0) 80%, rgba(0, 0, 0, 0) 100%);
}

.d-block {
  height: 60vh;
  object-fit: cover;
}

.search-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.search-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.search-title {
  color: white;
  font-weight: 600;
  font-size: 2rem;
}

.search-form :deep(.el-input-group__prepend) {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
}

.search-form :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.9);
}

.hot-keywords {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.keyword-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.keyword-tag {
  cursor: pointer;
  transition: all 0.3s;
}

.keyword-tag:hover {
  transform: translateY(-2px);
}

.section-header {
  margin-bottom: 3rem;
}

.section-title {
  font-size: 2.5rem;
  font-weight: 700;
  background: linear-gradient(45deg, #667eea, #764ba2);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 10px;
}

.section-subtitle {
  color: #666;
  font-size: 1.1rem;
}

.post-card {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  transition: all 0.3s;
  cursor: pointer;
  height: 100%;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.post-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.post-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.post-card:hover .post-image img {
  transform: scale(1.05);
}

.post-overlay {
  position: absolute;
  top: 0;
  right: 0;
  padding: 15px;
}

.post-stats {
  display: flex;
  gap: 15px;
}

.post-stats span {
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 5px 10px;
  border-radius: 15px;
  font-size: 12px;
}

.post-content {
  padding: 20px;
}

.post-title {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: #333;
}

.post-location {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
}

.post-location i {
  color: #667eea;
}

.post-excerpt {
  color: #888;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 15px;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
}

.author-name {
  font-size: 14px;
  color: #666;
}

.post-date {
  font-size: 12px;
  color: #999;
}

.destination-card {
  border-radius: 15px;
  overflow: hidden;
  height: 200px;
  position: relative;
  cursor: pointer;
  transition: all 0.3s;
}

.destination-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.destination-image {
  width: 100%;
  height: 100%;
  position: relative;
}

.destination-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.destination-card:hover .destination-image img {
  transform: scale(1.05);
}

.destination-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  color: white;
  padding: 20px;
}

.destination-name {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 5px;
}

.destination-posts {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
}

.stats-section {
  background: #f8f9fa;
}

.stat-item {
  padding: 20px;
}

.stat-number {
  font-size: 2.5rem;
  font-weight: bold;
  background: linear-gradient(45deg, #667eea, #764ba2);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 5px;
}

.stat-label {
  color: #666;
  font-size: 14px;
  margin: 0;
}

@media (max-width: 768px) {
  .banner {
    height: 50vh;
  }
  
  .d-block {
    height: 50vh;
  }
  
  .search-card {
    padding: 20px;
  }
  
  .search-title {
    font-size: 1.5rem;
  }
  
  .section-title {
    font-size: 2rem;
  }
  
  .hot-keywords {
    justify-content: center;
  }
}
</style>


